<?php
if (session_id() == '') {
	session_name("WESTCONWS_2013");
	session_start();
}
error_reporting(E_ALL & ~E_NOTICE);
$path_root_dbClass = dirname(__FILE__);
$DS = DIRECTORY_SEPARATOR;
$path_root_dbClass = "{$path_root_dbClass}{$DS}..{$DS}";
require_once "{$path_root_dbClass}lib{$DS}DataBaseClass.php";
require_once "{$path_root_dbClass}lib{$DS}canvas.php";
require_once("{$path_root_dbClass}lib{$DS}nusoap{$DS}lib{$DS}nusoap.php");
class defaultClass {
	protected $dbConn;
	protected $values;
	protected $files;
	protected $sort_field;
	protected $sort_direction;
	protected $limit_start;
	protected $limit_max;
	protected $post;
	//protected $emailAuthWS = "helena@multisupriinformatica.com.br";
	//protected $senhaAuthWS = "080770";
	
	/*protected $emailAuthWS = "teste@alcateia.com.br";
	protected $senhaAuthWS = "nqvtyn";
	protected $EmpresaIDWS = "2";
	protected $ParceiroIDWS = "94073";
	protected $urlWS = "http://www.alcasoft.com.br/PoweredBy/PoweredByWebService.asmx";
	*/
	protected $emailAuthWS;
	protected $senhaAuthWS;
	protected $EmpresaIDWS;
	protected $ParceiroIDWS;
	protected $urlWS;
	protected $bd_producao;
	protected $uf = array(
		"AC"=>"Acre"
		,"AL"=>"Alagoas"
		,"AM"=>"Amazonas"
		,"AP"=>"Amapa"
		,"BA"=>"Bahia"
		,"CE"=>"Ceará"
		,"DF"=>"Distrito Federal"
		,"ES"=>"Espirito Santo"
		,"GO"=>"Goiás"
		,"MA"=>"Maranhão"
		,"MG"=>"Minas Gerais"
		,"MS"=>"Mato Grosso do Sul"
		,"MT"=>"Mato Grosso"
		,"PA"=>"Pará"
		,"PB"=>"Paraíba"
		,"PE"=>"Pernambuco"
		,"PI"=>"Piauí"
		,"PR"=>"Parana"
		,"RJ"=>"Rio de Janeiro"
		,"RN"=>"Rio Grande do Norte"
		,"RO"=>"Rondônia"
		,"RR"=>"Roraima"
		,"RS"=>"Rio Grande do Sul"
		,"SC"=>"Santa Catarina"
		,"SE"=>"Sergipe"
		,"SP"=>"São Paulo"
		,"TO"=>"Tocantins"
	);
	
	protected $meses = array(
		"01"=>"Janeiro"
		,"02"=>"Fevereiro"
		,"03"=>"Março"
		,"04"=>"Abril"
		,"05"=>"Maio"
		,"06"=>"Junho"
		,"07"=>"Julho"
		,"08"=>"Agosto"
		,"09"=>"Setembro"
		,"10"=>"Outubro"
		,"11"=>"Novembro"
		,"12"=>"Dezembro"
	);
	protected $aDia_semana = array(
		'Domingo'
		,'Segunda-Feira'
		,'Terça-Feira'
		,'Quarta-Feira'
		,'Quinta-Feira'
		,'Sexta-Feira'
		,'Sábado'
	);
	protected $aDia_semana_res = array(
		'Dom'
		,'Seg'
		,'Ter'
		,'Qua'
		,'Qui'
		,'Sex'
		,'Sáb'
	);
	public function __construct() {
		$this->dbConn = new DataBaseClass();
		$this->setConfig();
	}
	public function setConfig(){
		$sql = array();
		$sql[] = "
			SELECT	*
			FROM	tb_configuracao
			WHERE	configuracao_id = '1'
		";
		
		$result = $this->dbConn->db_query(implode('\n',$sql));
		if($result['total'] > 0){
			$rs=$this->dbConn->db_fetch_assoc($result['result']);
			$this->emailAuthWS = $rs['configuracao_user'];
			$this->senhaAuthWS = $rs['configuracao_pass'];
			$this->urlWS = $rs['configuracao_urlWS'];
			$this->bd_producao = $rs['configuracao_bd_producao'];
			
		}
	}
	
	protected function corrigeHoraLocalizacao($aArr){
		$aData = array();
		$aHora = array();
		
		$dt = explode(" ",$aArr['datahora']);
		$hr = explode(" ",$aArr['datahora']);
		
		$aData = reset($dt);
		$aHora = end($hr);
		$aData = explode("-",$aData);
		$aHora = explode(":",$aHora);

		$ano = $aData[0];
		$mes = $aData[1];
		$dia = $aData[2];

		$hora = $aHora[0];
		$minuto = $aHora[1];
		$segundo = $aHora[2];

		if($segundo > 30){
			$aArr['datahora'] = date("Y-m-d H:i:s",  mktime($hora, ($minuto + 1), 0, $mes, $dia, $ano));
		}
		return $aArr['datahora'];
	}
	
	public function diffDateHour($d1, $d2, $type='MI'){
		$d1 = explode(" ", $d1);
		$d2 = explode(" ", $d2);
		
		$h1 = explode(":",$d1[1]);
		$h2 = explode(":",$d2[1]);
		
		$d1 = explode("-", $d1[0]);
		$d2 = explode("-", $d2[0]);
		switch ($type){
			case 'A':
				$X = 31536000;
			break;
			case 'M':
				$X = 2592000;
			break;
			case 'D':
				$X = 86400;
			break;
			case 'H':
				$X = 3600;
			break;
			case 'MI':
				$X = 60;
			break;
			default:
				$X = 1;
			break;
		}
		$nD2 = mktime($h2[0], $h2[1], $h2[2], $d2[1], $d2[2], $d2[0]);
		$nD1 = mktime($h1[0], $h1[1], $h1[2], $d1[1], $d1[2], $d1[0]);
		$diff = (($nD2 - $nD1)/$X);
		return floor($diff);
	}
	
	public function getADia_semana() {
		return $this->aDia_semana;
	}
	
	public function getADia_semana_res() {
		return $this->aDia_semana_res;
	}
	public function normaliza($string) {
		$a = 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûýýþÿŔŕ';
		$b = 'aaaaaaaceeeeiiiidnoooooouuuuybsaaaaaaaceeeeiiiidnoooooouuuyybyRr';
		$string = $this->utf8Encode2Decode($string);
		$string = strtr($string, $this->utf8Encode2Decode($a), $b);
		$string = str_replace(' ', '', $string);
		$string = strtolower($string);
		return $this->utf8Encode2Decode($string);
	}
	public function toNormaliza($string) {
		$array1 = array("á", "à", "â", "ã", "ä", "é", "è", "ê", "ë", "í", "ì", "î", "ï", "ó", "ò", "ô", "õ", "ö", "ú", "ù", "û", "ü", "ç"
			, "Á", "À", "Â", "Ã", "Ä", "É", "È", "Ê", "Ë", "Í", "Ì", "Î", "Ï", "Ó", "Ò", "Ô", "Õ", "Ö", "Ú", "Ù", "Û", "Ü", "Ç",'º','ª','-');
		$array2 = array("a", "a", "a", "a", "a", "e", "e", "e", "e", "i", "i", "i", "i", "o", "o", "o", "o", "o", "u", "u", "u", "u", "c"
			, "A", "A", "A", "A", "A", "E", "E", "E", "E", "I", "I", "I", "I", "O", "O", "O", "O", "O", "U", "U", "U", "U", "C",'','','');
		$string = str_replace(' ', '_', $string);
		return strtolower(str_replace($array1, $array2, $string));
	}
	public function toNormaliza2($string) {
		$array1 = array("á", "à", "â", "ã", "ä", "é", "è", "ê", "ë", "í", "ì", "î", "ï", "ó", "ò", "ô", "õ", "ö", "ú", "ù", "û", "ü", "ç"
			, "Á", "À", "Â", "Ã", "Ä", "É", "È", "Ê", "Ë", "Í", "Ì", "Î", "Ï", "Ó", "Ò", "Ô", "Õ", "Ö", "Ú", "Ù", "Û", "Ü", "Ç",'º','ª','-');
		$array2 = array("a", "a", "a", "a", "a", "e", "e", "e", "e", "i", "i", "i", "i", "o", "o", "o", "o", "o", "u", "u", "u", "u", "c"
			, "A", "A", "A", "A", "A", "E", "E", "E", "E", "I", "I", "I", "I", "O", "O", "O", "O", "O", "U", "U", "U", "U", "C",'','','');
		$string = str_replace(' ', '_', $string);
		return str_replace($array1, $array2, $string);
	}
	public function registerSession($arr){
		if(is_array($arr)&&count($arr) > 0){
			foreach($arr AS $k=>$v){
				$_SESSION['WESTCONWS'][$k] = $v;
			}
		}
	}
	public function unRegisterSession($arr){
		if(is_array($arr)&&count($arr) > 0){
			foreach($arr AS $k=>$v){
				unset($_SESSION['WESTCONWS'][$k]);
			}
		}
	}
	
	public function unRegisterSessionAll(){
		$arr = $this->getSessions();
		if(is_array($arr)&&count($arr) > 0){
			foreach($arr AS $k=>$v){
				unset($_SESSION['WESTCONWS'][$k]);
			}
		}
	}

	public function getSessions(){
		return $_SESSION['WESTCONWS'];
	}

	public function verifyLogon(){
		$session = $this->getSessions();
		if(isset($session['usuario_login'])&&trim($session['usuario_login'])!=''){
			header('Location: bootstrap.php');
		}
	}
	public function verifyLogin($mobile=false){
		$session = $this->getSessions();
		if(!isset($session['usuario_login'])&&trim($session['usuario_login'])==''){
			if(!$mobile){
				header('Location: ../index.php');
			}else{
				header('Location: ../mobile/index.php');
			}
		}
	}
	public function verifyLoginAjax(){
		$session = $this->getSessions();
		if(!isset($session['usuario_login'])&&trim($session['usuario_login'])==''){
			return "CMD_FAILED";
		}else{
			return "CMD_SUCCESS";
		}
	}
	public function simple_curl($url,$post=array(),$get=array()){
		$url = explode('?',$url,2);
		if(count($url)===2){
			$temp_get = array();
			parse_str($url[1],$temp_get);
			$get = array_merge($get,$temp_get);
		}

		$ch = curl_init($url[0]."?".http_build_query($get));
		curl_setopt ($ch, CURLOPT_POST, 1);
		curl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query($post));
		curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		return curl_exec ($ch);
	}
	public function busca_cep($cep){  
		$resultado = @file_get_contents('http://republicavirtual.com.br/web_cep.php?cep='.urlencode($cep).'&formato=query_string');  
		if(!$resultado){  
			$resultado = "&resultado=0&resultado_txt=erro+ao+buscar+cep";  
		}  
		parse_str($resultado, $retorno);

		return $this->utf8_array_encode($retorno);  
	} 
	public function getEnderecoByCep($cep){
		$path_root_dbClass = dirname(__FILE__);
		$DS = DIRECTORY_SEPARATOR;
		$path_root_dbClass = "{$path_root_dbClass}{$DS}..{$DS}";
		include_once("{$path_root_dbClass}lib{$DS}phpQuery-onefile.php");
		$aDados = $this->getSavedEnderecoByCep(str_replace("-","",$cep));
		if(count($aDados)<1){
			$html = $this->simple_curl('http://m.correios.com.br/movel/buscaCepConfirma.do',array(
				'cepEntrada'=>$cep,
				'tipoCep'=>'',
				'cepTemp'=>'',
				'metodo'=>'buscarCep'
			));

			phpQuery::newDocumentHTML($html, $charset = 'utf-8');
			$dados = 
				array(
					'logradouro'=> utf8_decode(trim(pq('.caixacampobranco .resposta:contains("Logradouro: ") + .respostadestaque:eq(0)')->html())),
					'bairro'=> utf8_decode(trim(pq('.caixacampobranco .resposta:contains("Bairro: ") + .respostadestaque:eq(0)')->html())),
					'cidade/uf'=> utf8_decode(trim(pq('.caixacampobranco .resposta:contains("Localidade / UF: ") + .respostadestaque:eq(0)')->html())),
					'cep'=> utf8_decode(trim(pq('.caixacampobranco .resposta:contains("CEP: ") + .respostadestaque:eq(0)')->html()))
				);
			$dados['cidade/uf'] = explode('/',$dados['cidade/uf']);
			$dados['cidade'] = trim($dados['cidade/uf'][0]);
			$dados['uf'] = trim($dados['cidade/uf'][1]);
			if(trim($dados['logradouro'])==""){
				$resultado_busca = $this->busca_cep(str_replace("-","",$cep));
				$dados = 
					array(
						'logradouro'=> "{$resultado_busca['tipo_logradouro']} {$resultado_busca['logradouro']}",
						'bairro'=> $resultado_busca['bairro'],
						'cidade/uf'=> "{$resultado_busca['cidade']}/{$resultado_busca['uf']}",
						'cep'=> $cep
					);
				$dados['cidade/uf'] = explode('/',$dados['cidade/uf']);
				$dados['cidade'] = trim($dados['cidade/uf'][0]);
				$dados['uf'] = trim($dados['cidade/uf'][1]);
			}
			$this->insertCep($dados);
		}else{
			$dados = array(
				'logradouro'=> $aDados['cep_logradouro'],
				'bairro'=> $aDados['cep_bairro'],
				'cidade/uf'=> $aDados['cep_cidade']."/".$aDados['cep_estado'],
				'cep'=>  $aDados['cep_cod'],
			);
			$dados['cidade/uf'] = explode('/',$dados['cidade/uf']);
			$dados['cidade'] = trim($dados['cidade/uf'][0]);
			$dados['uf'] = trim($dados['cidade/uf'][1]);
		}
		return $this->utf8_array_encode($dados);
	}
	public function insertCep($dados){
		$sql = array();
		$dados['cep'] = $this->dbConn->db_escape_string($dados['cep']);
		$dados['logradouro'] = $this->dbConn->db_escape_string($dados['logradouro']);
		$dados['bairro'] = $this->dbConn->db_escape_string($dados['bairro']);
		$dados['cidade'] = $this->dbConn->db_escape_string($dados['cidade']);
		$dados['uf'] = $this->dbConn->db_escape_string($dados['uf']);
		$sql[] = "
			INSERT INTO gc_cep SET
				cep_cod = '{$dados['cep']}'
				,cep_logradouro = '{$dados['logradouro']}'
				,cep_bairro = '{$dados['bairro']}'
				,cep_cidade = '{$dados['cidade']}'
				,cep_estado = '{$dados['uf']}'
		";
		$this->dbConn->db_execute(implode('\n',$sql));
	}
	public function getSavedEnderecoByCep($cep){
		$rs = array();
		$sql = array();
		$sql[] = "
			SELECT	*
			FROM	gc_cep
			WHERE	cep_cod = '{$cep}'
		";
		$result = $this->dbConn->db_query(implode('\n',$sql));
		if($result['total'] > 0){
			$rs=$this->dbConn->db_fetch_assoc($result['result']);
		}
		return $this->utf8_array_encode($rs);
	}
	
	public function getUrl_base() {
		return $this->dbConn->getUrl_base();
	}
	public function setValues($values) {
		$this->values = $values;
	}
	
	public function setPost($post) {
		$this->post = $post;
	}
	
	public function getUf() {
		return $this->uf;
	}
	
	public function getMeses() {
		return $this->meses;
	}

				
	public function setFiles($files) {
		$this->files = $files;
	}
	public function utf8Encode2Decode($string) {
		/*if (strtoupper(mb_detect_encoding($string . "x", 'UTF-8, ISO-8859-1')) == 'UTF-8') {
			return $string;
		} else {
			return utf8_encode($string);
		}*/
		return $string;
	}

	public function utf8_array_encode($input) {
		$return = array();
		foreach ($input as $key => $val) {
			if (is_array($val)) {
				$return[$key] = $this->utf8_array_encode($val);
			} else {
				$return[$key] = $this->utf8Encode2Decode($val);
			}
		}
		return $return;
	}

	public function antiInjection($input) {
		if(is_array($input)){
			foreach ($input as $key => $val) {
				$sql = preg_replace("/(from|select|insert|delete|where|drop table|show tables|#|\*|--|\\\\)/", "", $val);
				$sql = trim($sql);
				$sql = (get_magic_quotes_gpc()) ? $sql : addslashes($sql);
				$input[$key] = $sql;
			}
			return $input;
		}
		$sql = preg_replace("/(from|select|insert|delete|where|drop table|show tables|#|\*|--|\\\\)/", "", $input);
		$sql = trim($sql);
		$sql = (get_magic_quotes_gpc()) ? $sql : addslashes($sql);
		return $sql;
	}
	public function alert($msg,$url=''){
		$aScript = array();
		$aScript[] = '<script type="text/javascript">';
		$aScript[] = "alert('{$msg}');";
		$aScript[] = (!is_null($url)&&trim($url)!='')?"window.location.href = '{$url}';":'';
		$aScript[] = '</script>';
		echo implode("\r\n",$aScript);
	}
	public function consoleLog($mixed=''){
		$msg = print_r($mixed,true);
		$aScript = array();
		$aScript[] = '<script type="text/javascript">';
		$aScript[] = "console.log('{$msg}');";
		$aScript[] = '</script>';
		echo implode("\r\n",$aScript);
	}
	public function debug($mixed){
		echo "<pre>".print_r($mixed,true)."</pre>";
	}
	public function dateDB2BR($date) {
		return preg_replace(
			"/([0-9]{4})-([0-9]{2})-([0-9]{2})/i",
			"$3/$2/$1",
			$date
		);
	}
	public function formatHora($date){
		return preg_replace(
				"/([0-9]{2})([0-9]{2})([0-9]{2})/i",
				"$1:$2:$3",
				$date
			);
	}
	public function dateBR2DB($date) {
		if(strlen($date)==10){
			return preg_replace(
				"/([0-9]{2})\/([0-9]{2})\/([0-9]{4})/i",
				"$3-$2-$1",
				$date
			);
		}else if(strlen($date)>7 && strlen($date)<10){
			$aDate = explode("/",$date);
			$dia = str_pad($aDate[0], 2, "0", STR_PAD_LEFT);
			$mes = str_pad($aDate[1], 2, "0", STR_PAD_LEFT);
			$ano = str_pad($aDate[2], 4, "0", STR_PAD_LEFT);
			return "{$ano}-{$mes}-{$dia}"; 
		}
		return '';
	}
	public function dateDB2BRTime($date) {
		return preg_replace(
			"/([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})/i",
			"$3/$2/$1 $4:$5:$6",
			$date
		);
	}
	public function dateDB2BRTime2($date) {
		return preg_replace(
			"/([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2})/i",
			"$3/$2/$1 $4:$5",
			$date
		);
	}

	public function dateBR2DBTime($date) {
		return preg_replace(
			"/([0-9]{2})\/([0-9]{2})\/([0-9]{4}) ([0-9]{2}):([0-9]{2}):([0-9]{2})/i",
			"$3-$2-$1 $4:$5:$6",
			$date
		);
	}
	public function timeSum($aHora){
		$hora = 0;
		$minuto = 0;
		$segundo = 0;
		if(is_array($aHora)&&count($aHora)){
			foreach($aHora as $v){
				$tem = explode(":",$v);
				$hora+=$tem[0];
				$minuto+=$tem[1];
				$segundo+=(isset($tem[2]))?$tem[2]:0;
			}
		}
		$secMin = floor($segundo/60);
		$segundo = $segundo - ($secMin * 60);
		
		$minuto += $secMin;
		$horaMin = floor($minuto/60);
		$hora+= $horaMin;
		$minuto = $minuto - ($horaMin * 60);
		
		$hora = str_pad($hora,2,'0',STR_PAD_LEFT);
		$minuto = str_pad($minuto,2,'0',STR_PAD_LEFT);
		$segundo = str_pad($segundo,2,'0',STR_PAD_LEFT);
		
		return "{$hora}:{$minuto}:{$segundo}";
	}
	public function SecToTime($sec,$bStyle=false){
		$style='style="color:#00F !important;"';
		$sinal = '+';
		if($sec < 0){
			$sec = $sec * -1;
			$style='style="color:#F00 !important;"';
			$sinal = '-';
		}
		
		$sql = "SELECT SEC_TO_TIME({$sec}) AS hora";
		$qry = $this->dbConn->db_query($sql);
		$rs=$this->dbConn->db_fetch_assoc($qry['result']);
		if($bStyle){
			return "<span {$style}>{$sinal} {$rs['hora']}</span>";
		}else{
			return $rs['hora'];
		}
		
	}
	public function diasemana($data,$ec=true) {
		$data = explode("-",$data);
		$ano =  $data[0];
		$mes =  $data[1];
		$dia =  $data[2];
		$diasemana = date("w", mktime(0,0,0,$mes,$dia,$ano) );
		switch($diasemana) {
			case"0": $diasemana = "Domingo"; break;
			case"1": $diasemana = "Segunda"; break;
			case"2": $diasemana = "Terça";   break;
			case"3": $diasemana = "Quarta";  break;
			case"4": $diasemana = "Quinta";  break;
			case"5": $diasemana = "Sexta";   break;
			case"6": $diasemana = "Sábado";  break;
		}
		if($ec){
			echo $diasemana;
		}else{
			return $diasemana;
		}
	}
	public function getIntDiaSemana($diasemana){
		switch($diasemana) {
			case "Domingo": $diasemana = "0";       break;
			case "Segunda-Feira": $diasemana = "1"; break;
			case "Terça-Feira": $diasemana = "2";   break;
			case "Quarta-Feira": $diasemana = "3";  break;
			case "Quinta-Feira": $diasemana = "4";  break;
			case "Sexta-Feira": $diasemana = "5";   break;
			case "Sábado": $diasemana = "6";        break;
		}
		return $diasemana;
	}
	public function getSeqAleatoria($caracs=8){
		$CaracteresAceitos = 'abcdefghijklmnopqrstuvwxyz';
		$CaracteresAceitos .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
		$CaracteresAceitos .= '0123456789';
		$max = strlen($CaracteresAceitos)-1;
		$password = null;
		for($i=0; $i < $caracs; $i++) {
			$password .= $CaracteresAceitos{mt_rand(0, $max)};
		}
		return $password;
	}
	public function HexToRGB($hex) {
		$hex = trim(str_replace("#", "", $hex));
		$color = array();
		if(strlen($hex) == 3) {
			$color['r'] = hexdec(substr($hex, 0, 1) . $r);
			$color['g'] = hexdec(substr($hex, 1, 1) . $g);
			$color['b'] = hexdec(substr($hex, 2, 1) . $b);
		}
		else if(strlen($hex) == 6) {
			$color['r'] = hexdec(substr($hex, 0, 2));
			$color['g'] = hexdec(substr($hex, 2, 2));
			$color['b'] = hexdec(substr($hex, 4, 2));
		}
		return $color;
	}
	public function RGBToHex($r, $g, $b) {
		$hex = "#";
		$hex.= str_pad(dechex($r), 2, "0", STR_PAD_LEFT);
		$hex.= str_pad(dechex($g), 2, "0", STR_PAD_LEFT);
		$hex.= str_pad(dechex($b), 2, "0", STR_PAD_LEFT);
		return $hex;
	}
	public function escape_string($string) {
		return $this->dbConn->db_escape_string($string);
	}
	public function escape_string_array_encode($input) {
		$return = array();
		foreach ($input as $key => $val) {
			if (is_array($val)) {
				$return[$key] = $this->escape_string_array_encode($val);
			} else {
				$return[$key] = $this->escape_string($val);
			}
		}
		return $return;
	}
	public function limpaDoc($doc){
		return str_replace(array(".","/","-"),"",$doc);
	}
	public function limpaMoeda($moeda){
		$moeda = str_replace(".","",$moeda);
		$moeda = trim(str_replace(",",".",$moeda));
		return trim($moeda);
	}
	public function limpaTel($tel){
		return trim(str_replace(array("(",")"," ","-"),"",$tel));
	}
	public function limpaCep($cep){
		return trim(str_replace(array("(",")"," ","-"),"",$cep));
	}
	
	public function autenticaWs(){
		$client = new nusoap_client("http://www.alcasoft.com.br/PoweredBy/PoweredByWebService.asmx?wsdl",true);
		$err = $client->getError();
		$xml[] = '<AutenticacaoHeader xmlns="http://www.alcasoft.com.br/ws">';
		$xml[] = '<Email>'.$this->emailAuthWS.'</Email>';
		$xml[] = '<Senha>'.$this->senhaAuthWS.'</Senha>';
		$xml[] = '</AutenticacaoHeader>';
		$client->setHeadersUser(implode("\n",$xml));
		$client->soap_defencoding = 'UTF-8';
		$result = $client->call('AutenticarUsuario',array(),'http://www.alcasoft.com.br/ws');
		if ($client->fault) {
			return false;
		}
		$err = $client->getError();
		if ($err) {
			return false;
		}
		return $result['AutenticarUsuarioResult'];
	}
	
	public function arrayTrim($input){
		$return = array();
		foreach ($input as $key => $val) {
			if (is_array($val)) {
				$return[$key] = $this->arrayTrim($val);
			} else {
				$return[$key] = trim($val);
			}
		}
		return $return;
	}
	
	public function currencyDB2BR($number,$decimals=2){
		return number_format($number, $decimals, ",", ".");
	}
	public function currencyBR2DB($number){
		$number = str_replace(".","",$number);
		return str_replace(",",".",$number);
	}
}